package com.jielihaofeng.shop.security.config.bean;

import com.wf.captcha.*;
import com.wf.captcha.base.Captcha;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.awt.*;
import java.io.OutputStream;
import java.util.Objects;

/**
 * @description 配置文件
 * @author Johnnie Wind
 * @date 2021/2/27 09:20
 */
@Data
public class LoginProperties {
    // 账号单用户登录
    private boolean singleLogin = false;

    // 登录验证码
    private LoginCode loginCode;

    // 用户信息缓存
    private boolean cacheEnable;

    public boolean isSingleLogin() {
        return singleLogin;
    }

    public boolean isCacheEnable() {
        return cacheEnable;
    }

    /**
     * @description 获取验证码
     * @author Johnnie Wind
     * @date 2021/2/27 9:53
     * @Param []
     * @return com.wf.captcha.base.Captcha
     */
    public Captcha getCaptcha() {
        if (Objects.isNull(loginCode)) {
            loginCode = new LoginCode();
            if (Objects.isNull(loginCode.getCodeType())) {
                loginCode.setCodeType(LoginCodeEnum.arithmetic);
            }
        }
        return switchCaptcha(loginCode);
    }

    /**
     * @description 依据配置信息生成验证码
     * @author Johnnie Wind
     * @date 2021/2/27 9:44
     * @Param [loginCode]
     * @return com.wf.captcha.base.Captcha
     */
    private Captcha switchCaptcha(LoginCode loginCode) {
        Captcha captcha;
        synchronized (this) {
            switch (loginCode.getCodeType()) {
                case arithmetic:
                    // 算数类型
                    captcha = new ArithmeticCaptcha(loginCode.getWidth(), loginCode.getHeight());
                    // 几位数运算，默认是两位
                    captcha.setLen(loginCode.getLenth());
                    break;
                case chinese:
                    captcha = new ChineseCaptcha(loginCode.getWidth(), loginCode.getHeight());
                    captcha.setLen(loginCode.getLenth());
                    break;
                case chinese_gif:
                    captcha = new ChineseGifCaptcha(loginCode.getWidth(), loginCode.getHeight());
                    captcha.setLen(loginCode.getLenth());
                    break;
                case gif:
                    captcha = new GifCaptcha(loginCode.getWidth(), loginCode.getHeight());
                    captcha.setLen(loginCode.getLenth());
                    break;
                case spec:
                    captcha = new SpecCaptcha(loginCode.getWidth(), loginCode.getHeight());
                    captcha.setLen(loginCode.getLenth());
                    break;
                default:
                    System.err.println("验证码配置错误");
                    captcha = new ArithmeticCaptcha(loginCode.getWidth(), loginCode.getHeight());
                    // 几位数运算，默认是两位
                    captcha.setLen(loginCode.getLenth());
                    break;
            }
        }
        // 根据配置设置字体
        if (loginCode.getFontSize()!=0) {
            captcha.setFont(new Font(loginCode.getFontName(),Font.PLAIN, loginCode.getFontSize()));
        }
        return captcha;
    }

}
